clear
set matsize 10000
use "${data}IndirectSurveyDataReplication.dta", clear

keep if complete == 1

estimates clear
*keep if PIB_leader & PIB_HH ~=.
label define ranks 1 "1"  2 "2" 3 "3" 

foreach x in  PIB IPW PCA {

bysort ejnclnmid: egen rank_HH_`x' = rank(`x'_HH), unique
label values rank_HH_`x' ranks
bysort ejnclnmid: egen rank_leader_`x' = rank(`x'_leader), unique
label values rank_leader_`x' ranks
}


estpost tab rank_leader_PIB rank_HH_PIB
esttab . using "${outputs}error_rank.tex", replace cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment collabels("" "" "")  noobs ///
  prehead( & \multicolumn{3}{c}{Household responses} \\ ) ///
  posthead(Leader responses & \\ \hline \multicolumn{3}{l}{Simple index} \\ ) ///
  postfoot(\hline) 

  estpost tab rank_leader_IPW rank_HH_IPW
esttab .  using "${outputs}error_rank.tex", append cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment  collabels("" "" "")  noobs ///
 posthead(\hline \multicolumn{3}{l}{Inverse proportion weighted index} \\ ) ///
postfoot(\hline)  
  
estpost tab rank_leader_PCA rank_HH_PCA
esttab .  using "${outputs}error_rank.tex", append cell(pct(fmt(%5.1f))) unstack nonumber nomtitles nodepvars nonumbers ///
 label fragment  collabels("" "" "") noobs ///
 posthead(\hline \multicolumn{3}{l}{Principle components index} \\ ) ///
 postfoot( \hline \hline \end{tabular} \begin{tablenotes}[para,flushleft] \footnotesize{Cells show percentages in each category. ///
 Rankings are unique, with the lowest value going to the poorest household.  In the event that there are ties -- which occurs for ///
 about 30 percent of the households -- the ties are broken arbitrarily.} \end{tablenotes} )

 
 